import { createSlice } from "@reduxjs/toolkit";

const channelStore = createSlice({
    name: "channel",
    initialState: {
        channelList: [],
    },
    reducers: {
        setChannel: (state, action) => {
            state.channelList = action.payload;
        }
    }
})

// 异步请求部分
const { setChannel } = channelStore.actions

const fetchChannelList = (): any => {
    const url = 'http://geek.itheima.net/v1_0/channels'
    return async (dispatch: any) => {
        const res = await fetch(url)
        const data = await res.json()
        dispatch(setChannel(data.data.channels))
    }
}

const channelReducer = channelStore.reducer

export { fetchChannelList }

export default channelReducer   